﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticPathPlanner.CUiAPPGeneralSettings
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.AutomaticPathPlanner;

public class CUiAPPGeneralSettings : 
  UserControl,
  INotifyPropertyChanged,
  IComparer<CApAPPZoneData>,
  IComponentConnector,
  IStyleConnector
{
  private KiAppPathPlanningParamsEx m_pathPlanningParameters;
  private CApAPPTaskManager m_taskManager;
  private List<List<CApAPPJointsData>> m_jointsDataList;
  private List<List<CApAPPJointsData>> m_gunJointsDataList;
  private List<List<string>> m_zoneNamesByParameter;
  private Dictionary<CApAPPRoboticTask, List<CApAPPZoneData>> m_zoneDataByRoboticTask;
  private string m_previousZoneName = "";
  private int m_nbZoneColumnVisible = 3;
  private Dictionary<CApAPPRoboticTask, SortedSet<string>> m_existingZoneByRoboticTask = new Dictionary<CApAPPRoboticTask, SortedSet<string>>();
  private CUiAPPGeneralSettings.LastClickType m_lastClickType;
  private bool m_isFirstInitialization = true;
  private bool m_isParentActivated;
  internal StackPanel spGeneralSettings;
  internal Grid generalSettingsGrid;
  internal CheckBox cbBackupOperation;
  internal CheckBox cbTimeOutIsActivated;
  internal TextBox tbTimeOutValue;
  internal TextBlock tbTimeOutValueUnit;
  internal TextBlock tbRobotNames;
  internal ComboBox cbRobotTaskNames;
  internal Grid gRobot;
  internal CheckBox rbJointWeights;
  internal CheckBox rbZones;
  internal DataGrid fgJointsWeight_DataGrid;
  internal TextBlock tbGunWeight;
  internal DataGrid fgGunJointsWeight_DataGrid;
  internal RadioButton rbAllMotionTypes;
  internal RadioButton rbSelectMotionTypes;
  internal ComboBox cbZoneMotionChoice;
  internal DataGrid fgZone;
  internal Button btnRemoveZone;
  internal Button btnAddZone;
  private bool _contentLoaded;

  public event EventHandler RestoreDefaultValuesTriggered;

  public event PropertyChangedEventHandler PropertyChanged;

  protected void NotifyPropertyChanged(string info)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(info));
  }

  public void Initialize(
    KiAppPathPlanningParamsEx pathPlanningParameters,
    CApAPPTaskManager taskManager)
  {
    this.m_pathPlanningParameters = pathPlanningParameters;
    this.m_taskManager = taskManager;
    if (this.m_isFirstInitialization)
    {
      this.InitializeComponent();
      this.m_isFirstInitialization = false;
    }
    this.tbTimeOutValueUnit.Text = TxApplication.Options.Units.TimeUnitName;
    this.cbTimeOutIsActivated.IsChecked = new bool?(this.m_pathPlanningParameters.TimeOutIsActivated);
    this.tbTimeOutValue.IsEnabled = this.cbTimeOutIsActivated.IsChecked.Value;
    this.tbTimeOutValue.Text = Convert.ToString(this.m_pathPlanningParameters.TimeOut / (TxApplication.Options.Units.TimeMultiplier == 0.0 ? 1.0 : TxApplication.Options.Units.TimeMultiplier));
    this.cbBackupOperation.IsChecked = new bool?(this.m_pathPlanningParameters.BackUpOperationIsActivated);
    this.FillRobotCombo();
    if (this.m_taskManager.IsObjectFlowMode)
    {
      if (this.tbRobotNames.Visibility == Visibility.Collapsed)
        return;
      this.rbZones.Visibility = Visibility.Collapsed;
      this.rbJointWeights.Visibility = Visibility.Collapsed;
      this.gRobot.Visibility = Visibility.Collapsed;
      this.tbRobotNames.Visibility = Visibility.Collapsed;
      this.cbRobotTaskNames.Visibility = Visibility.Collapsed;
      if (this.generalSettingsGrid.RowDefinitions.Count <= 3)
        return;
      this.generalSettingsGrid.RowDefinitions[3].Height = new GridLength(0.0);
      this.generalSettingsGrid.RowDefinitions.Remove(this.generalSettingsGrid.RowDefinitions[3]);
    }
    else
    {
      this.InitJointData();
      this.FillJointsDataGrid();
      this.InitZoneData();
    }
  }

  public void SetParentIsActivated() => this.m_isParentActivated = true;

  private void InitJointData()
  {
    this.m_jointsDataList = new List<List<CApAPPJointsData>>();
    this.m_gunJointsDataList = new List<List<CApAPPJointsData>>();
    for (int index1 = 0; index1 < this.m_taskManager.CountTasks(); ++index1)
    {
      List<CApAPPJointsData> capAppJointsDataList1 = new List<CApAPPJointsData>();
      List<CApAPPJointsData> capAppJointsDataList2 = new List<CApAPPJointsData>();
      if (this.m_taskManager.Tasks[index1] is CApAPPRoboticTask)
      {
        CApAPPRoboticTask task = this.m_taskManager.Tasks[index1] as CApAPPRoboticTask;
        double[] drivingJointsWeight1 = task.DrivingJointsWeight;
        List<string> deviceJointNames = task.GetMainDeviceJointNames();
        bool flag1 = deviceJointNames.Count == drivingJointsWeight1.Length;
        for (int index2 = 0; index2 < drivingJointsWeight1.Length; ++index2)
        {
          CApAPPJointsData capAppJointsData = new CApAPPJointsData(flag1 ? deviceJointNames[index2] : "j" + Convert.ToString(index2 + 1), drivingJointsWeight1[index2]);
          capAppJointsData.JointDataDidChanged += new JointDataDidChangedEventHandler(this.DrivingJointsWeightDidChanged);
          capAppJointsDataList1.Add(capAppJointsData);
        }
        this.m_jointsDataList.Add(capAppJointsDataList1);
        List<string> o_devicesJointNames = new List<string>();
        double[] drivingJointsWeight2 = task.GunAndAuxiliaryDrivingJointsWeight;
        task.GetExternalAxisJointNames(ref deviceJointNames, ref o_devicesJointNames);
        bool flag2 = deviceJointNames.Count == drivingJointsWeight2.Length;
        for (int index3 = 0; index3 < drivingJointsWeight2.Length; ++index3)
        {
          CApAPPJointsData capAppJointsData = new CApAPPJointsData(flag2 ? $"{deviceJointNames[index3]} ({o_devicesJointNames[index3]})" : "j" + Convert.ToString(index3 + 1), drivingJointsWeight2[index3]);
          capAppJointsData.JointDataDidChanged += new JointDataDidChangedEventHandler(this.DrivingJointsWeightDidChanged);
          capAppJointsDataList2.Add(capAppJointsData);
        }
        this.m_gunJointsDataList.Add(capAppJointsDataList2);
      }
    }
  }

  private void InitZoneData()
  {
    this.InitZoneNames();
    int selectedIndex = this.cbZoneMotionChoice.SelectedIndex;
    TxMotionType i_type = (TxMotionType) 2;
    if (this.rbAllMotionTypes.IsChecked.Value || selectedIndex == 0)
      i_type = (TxMotionType) 1;
    foreach (CApAPPTask task in this.m_taskManager.Tasks)
    {
      CApAPPRoboticTask capAppRoboticTask = task as CApAPPRoboticTask;
      string[][] zones = capAppRoboticTask.JointMotionZoneNames;
      CApAPPRoboticTask.EnsureDefaultZoneIsInZoneList(this.m_zoneNamesByParameter, ref zones);
      capAppRoboticTask.JointMotionZoneNames = zones;
      zones = capAppRoboticTask.LinearMotionZoneNames;
      CApAPPRoboticTask.EnsureDefaultZoneIsInZoneList(this.m_zoneNamesByParameter, ref zones);
      capAppRoboticTask.LinearMotionZoneNames = zones;
    }
    this.m_zoneDataByRoboticTask = new Dictionary<CApAPPRoboticTask, List<CApAPPZoneData>>();
    foreach (CApAPPTask task in this.m_taskManager.Tasks)
    {
      CApAPPRoboticTask key = task as CApAPPRoboticTask;
      List<List<string>> normalizedZones = this.ComputeNormalizedZones(key.GetMotionZoneNames(i_type));
      List<CApAPPZoneData> capAppZoneDataList = new List<CApAPPZoneData>();
      if (this.AreMotionZonesValid(normalizedZones))
      {
        foreach (List<string> stringList in normalizedZones)
        {
          CApAPPZoneData capAppZoneData = new CApAPPZoneData(stringList[0], stringList[1], stringList[2], this.m_zoneNamesByParameter);
          capAppZoneData.ZoneDataDidChanged += new ZoneDataDidChangedEventHandler(this.m_ZoneDataDidChanged);
          capAppZoneDataList.Add(capAppZoneData);
        }
      }
      else
      {
        string[][] zones = new string[0][];
        CApAPPRoboticTask.EnsureDefaultZoneIsInZoneList(this.m_zoneNamesByParameter, ref zones);
        key.SetMotionZoneNames((TxMotionType) 2, zones);
        key.SetMotionZoneNames((TxMotionType) 1, zones);
        List<string> normalizedZone = this.ComputeNormalizedZones(zones)[0];
        CApAPPZoneData capAppZoneData = new CApAPPZoneData(normalizedZone[0], normalizedZone[1], normalizedZone[2], this.m_zoneNamesByParameter);
        capAppZoneData.ZoneDataDidChanged += new ZoneDataDidChangedEventHandler(this.m_ZoneDataDidChanged);
        capAppZoneDataList.Add(capAppZoneData);
      }
      capAppZoneDataList.Sort((IComparer<CApAPPZoneData>) this);
      this.m_zoneDataByRoboticTask[key] = capAppZoneDataList;
      this.m_existingZoneByRoboticTask[key] = new SortedSet<string>();
    }
    this.UpdateZoneButtonVisibility();
    this.InitZoneGridColumns();
    this.UpdateZoneFlexGrid();
  }

  private List<List<string>> ComputeNormalizedZones(string[][] rawZones)
  {
    List<List<string>> normalizedZones = new List<List<string>>();
    foreach (string[] rawZone in rawZones)
      normalizedZones.Add(new List<string>()
      {
        ((IEnumerable<string>) rawZone).Count<string>() > 0 ? rawZone[0] : "",
        ((IEnumerable<string>) rawZone).Count<string>() > 1 ? rawZone[1] : "",
        ((IEnumerable<string>) rawZone).Count<string>() > 2 ? rawZone[2] : ""
      });
    return normalizedZones;
  }

  private void InitZoneGridColumns()
  {
    ObservableCollection<DataGridColumn> columns = this.fgZone.Columns;
    CApAPPRoboticTask selectedItem = this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask;
    List<string> zoneParameters = CApAPPUtilities.GetZoneParameters(selectedItem.Robot, selectedItem.Operation);
    this.m_nbZoneColumnVisible = zoneParameters.Count;
    for (int count = zoneParameters.Count; count < columns.Count; ++count)
      columns[count].Visibility = Visibility.Hidden;
  }

  private void InitZoneNames()
  {
    CApAPPRoboticTask selectedItem = this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask;
    List<string> zoneParameters = CApAPPUtilities.GetZoneParameters(selectedItem.Robot, selectedItem.Operation);
    selectedItem.ZoneParameters = zoneParameters.ToArray();
    TxMotionType motionType = this.rbAllMotionTypes.IsChecked.Value || this.cbZoneMotionChoice.SelectedIndex == 0 ? (TxMotionType) 1 : (TxMotionType) 2;
    this.m_zoneNamesByParameter = CApAPPUtilities.GetAllRobotsZone(selectedItem.Robot, selectedItem.Operation, zoneParameters, motionType);
    ObservableCollection<DataGridColumn> columns = this.fgZone.Columns;
    for (int index = 0; index < zoneParameters.Count; ++index)
    {
      string str = zoneParameters[index];
      columns[index].Header = (object) (str + ":");
    }
  }

  private void FillRobotCombo()
  {
    this.cbRobotTaskNames.ItemsSource = (IEnumerable) this.m_taskManager.Tasks;
  }

  private void FillJointsDataGrid()
  {
    if (this.m_jointsDataList == null)
      return;
    this.UpdateJointsFlexGrid();
  }

  private void UpdateJointsFlexGrid()
  {
    if (this.m_jointsDataList == null || this.cbRobotTaskNames.SelectedIndex < 0 || this.cbRobotTaskNames.SelectedIndex >= this.m_jointsDataList.Count)
      return;
    this.fgJointsWeight_DataGrid.ItemsSource = (IEnumerable) null;
    this.fgJointsWeight_DataGrid.ItemsSource = (IEnumerable) this.m_jointsDataList[this.cbRobotTaskNames.SelectedIndex];
    if (this.m_gunJointsDataList[this.cbRobotTaskNames.SelectedIndex].Count == 0)
    {
      this.fgGunJointsWeight_DataGrid.Height = 0.0;
      this.fgGunJointsWeight_DataGrid.Visibility = Visibility.Hidden;
      this.fgGunJointsWeight_DataGrid.Width = 0.0;
      this.gRobot.ColumnDefinitions[2].Width = GridLength.Auto;
      this.tbGunWeight.Text = $"{CApAPPMainWindowStringTable.AppExternalAxesWeightJointLabel} {CApAPPMainWindowStringTable.NoExternalAxisJointLabel}";
    }
    else
    {
      this.fgGunJointsWeight_DataGrid.ItemsSource = (IEnumerable) null;
      this.fgGunJointsWeight_DataGrid.ItemsSource = (IEnumerable) this.m_gunJointsDataList[this.cbRobotTaskNames.SelectedIndex];
    }
  }

  private void DrivingJointsWeightDidChanged(object sender = null, EventArgs args = null)
  {
    int selectedIndex = this.cbRobotTaskNames.SelectedIndex;
    double[] numArray1 = new double[this.m_jointsDataList[selectedIndex].Count];
    double[] numArray2 = new double[this.m_gunJointsDataList[selectedIndex].Count];
    for (int index = 0; index < this.m_jointsDataList[selectedIndex].Count; ++index)
      numArray1[index] = this.m_jointsDataList[selectedIndex][index].Weight;
    for (int index = 0; index < this.m_gunJointsDataList[selectedIndex].Count; ++index)
      numArray2[index] = this.m_gunJointsDataList[selectedIndex][index].Weight;
    (this.m_taskManager.Tasks[selectedIndex] as CApAPPRoboticTask).DrivingJointsWeight = numArray1;
    (this.m_taskManager.Tasks[selectedIndex] as CApAPPRoboticTask).GunAndAuxiliaryDrivingJointsWeight = numArray2;
  }

  private void btnRestoreDefaults_Click(object sender, RoutedEventArgs e)
  {
    EventHandler defaultValuesTriggered = this.RestoreDefaultValuesTriggered;
    if (defaultValuesTriggered == null)
      return;
    defaultValuesTriggered(sender, (EventArgs) e);
  }

  private void tbTimeOutValue_TextChanged(object sender, TextChangedEventArgs e)
  {
    if (!(this.tbTimeOutValue.Text != ""))
      return;
    double num = TxApplication.Options.Units.TimeMultiplier == 0.0 ? 1.0 : TxApplication.Options.Units.TimeMultiplier;
    try
    {
      this.m_pathPlanningParameters.TimeOut = (double) Convert.ToInt32(this.tbTimeOutValue.Text) * num;
      this.tbTimeOutValue.Text = Convert.ToString(this.m_pathPlanningParameters.TimeOut / num);
      this.tbTimeOutValue.SelectionStart = this.tbTimeOutValue.Text.Count<char>();
      this.tbTimeOutValue.SelectionLength = 0;
    }
    catch (FormatException ex)
    {
      this.tbTimeOutValue.Text = Convert.ToString(this.m_pathPlanningParameters.TimeOut / num);
    }
    catch (OverflowException ex)
    {
      this.tbTimeOutValue.Text = "0";
    }
  }

  private void tbTimeOutValue_FocusChanged(object sender, DependencyPropertyChangedEventArgs e)
  {
    if (!(this.tbTimeOutValue.Text == ""))
      return;
    this.tbTimeOutValue.Text = Convert.ToString(this.m_pathPlanningParameters.TimeOut / (TxApplication.Options.Units.TimeMultiplier == 0.0 ? 1.0 : TxApplication.Options.Units.TimeMultiplier));
  }

  private void tbTimeOutValue_PreviewTextInput(object sender, TextCompositionEventArgs e)
  {
    string text = e.Text;
    int result = 0;
    if (int.TryParse(text, out result))
      return;
    e.Handled = true;
  }

  private void dgWeightText_PreviewTextInput(object sender, TextCompositionEventArgs e)
  {
    string text = e.Text;
    int result = 0;
    if (int.TryParse(text, out result))
      return;
    e.Handled = true;
  }

  private void dgWeightText_TextChanged(object sender, RoutedEventArgs e)
  {
    int selectedIndex1 = this.fgJointsWeight_DataGrid.SelectedIndex;
    if (!(sender is TextBox textBox) || selectedIndex1 < 0 || !(textBox.Text != ""))
      return;
    int selectedIndex2 = this.cbRobotTaskNames.SelectedIndex;
    int int32 = Convert.ToInt32(textBox.Text);
    switch (int32)
    {
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
      case 10:
        if (selectedIndex2 >= this.m_taskManager.CountTasks())
          break;
        this.m_jointsDataList[selectedIndex2][selectedIndex1].Weight = (double) int32;
        this.DrivingJointsWeightDidChanged();
        break;
    }
  }

  private void btnAddZone_Click(object sender, RoutedEventArgs e)
  {
    CApAPPRoboticTask selectedItem = this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask;
    CApAPPZoneData i_dataList = (CApAPPZoneData) null;
    int selectedIndex = this.fgZone.SelectedIndex;
    this.fgZone.SelectedIndex = Math.Max(0, this.fgZone.SelectedIndex);
    if (this.m_zoneDataByRoboticTask[selectedItem].Count != 0)
      i_dataList = this.m_zoneDataByRoboticTask[selectedItem][this.fgZone.SelectedIndex];
    List<string> nextZoneNames = this.ComputeNextZoneNames(i_dataList);
    if (nextZoneNames.Count == 0)
      nextZoneNames = this.ComputeNextZoneNames(this.m_zoneDataByRoboticTask[selectedItem][0]);
    if (nextZoneNames.Count != 3)
      return;
    CApAPPZoneData capAppZoneData = new CApAPPZoneData(nextZoneNames[0], nextZoneNames[1], nextZoneNames[2], this.m_zoneNamesByParameter);
    capAppZoneData.ZoneDataDidChanged += new ZoneDataDidChangedEventHandler(this.m_ZoneDataDidChanged);
    this.m_zoneDataByRoboticTask[selectedItem].Add(capAppZoneData);
    this.m_zoneDataByRoboticTask[selectedItem].Sort((IComparer<CApAPPZoneData>) this);
    string uniqueName = capAppZoneData.UniqueName;
    if (!this.m_existingZoneByRoboticTask[selectedItem].Contains(uniqueName))
      this.m_existingZoneByRoboticTask[selectedItem].Add(uniqueName);
    this.fgZone.ItemsSource = (IEnumerable) null;
    this.fgZone.ItemsSource = (IEnumerable) this.m_zoneDataByRoboticTask[selectedItem];
    this.fgZone.Refresh();
    this.ZoneDataDidChanged();
    this.fgZone.SelectedIndex = selectedIndex >= 0 ? selectedIndex + 1 : 0;
  }

  private void btnRemoveZone_Click(object send, RoutedEventArgs e)
  {
    CApAPPRoboticTask selectedItem = this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask;
    int selectedIndex = this.fgZone.SelectedIndex;
    if (this.fgZone.SelectedIndex < this.m_zoneDataByRoboticTask[selectedItem].Count && this.fgZone.SelectedIndex > 0)
    {
      CApAPPZoneData capAppZoneData = this.m_zoneDataByRoboticTask[selectedItem][this.fgZone.SelectedIndex];
      string uniqueName = capAppZoneData.UniqueName;
      this.m_zoneDataByRoboticTask[selectedItem].Remove(capAppZoneData);
      this.m_zoneDataByRoboticTask[selectedItem].Sort((IComparer<CApAPPZoneData>) this);
      if (this.m_existingZoneByRoboticTask[selectedItem].Contains(uniqueName))
        this.m_existingZoneByRoboticTask[selectedItem].Remove(uniqueName);
      this.fgZone.ItemsSource = (IEnumerable) null;
      this.fgZone.ItemsSource = (IEnumerable) this.m_zoneDataByRoboticTask[selectedItem];
      this.fgZone.Refresh();
      this.fgZone.SelectedIndex = selectedIndex == 0 ? 0 : selectedIndex - 1;
    }
    this.ZoneDataDidChanged();
  }

  public List<string> AvailableZoneNames1 => this.GenerateAvailableZoneNames(0);

  public List<string> AvailableZoneNames2 => this.GenerateAvailableZoneNames(1);

  public List<string> AvailableZoneNames3 => this.GenerateAvailableZoneNames(2);

  private void cbZoneFlexGrid_DropdownClosed(object sender, EventArgs e)
  {
  }

  private void cbZoneFlexGrid_DropDown(object sender, EventArgs e)
  {
  }

  private void cbRobotTaskNames_DropDownClosed(object sender, EventArgs e)
  {
    this.UpdateJointsFlexGrid();
    this.UpdateZoneFlexGrid();
    this.UpdateZoneButtonVisibility();
  }

  private void cbBackupOperation_Click(object sender, RoutedEventArgs e)
  {
    this.m_pathPlanningParameters.BackUpOperationIsActivated = this.cbBackupOperation.IsChecked.Value;
  }

  private void cbTimeOutIsActivated_Click(object sender, RoutedEventArgs e)
  {
    bool flag = this.cbTimeOutIsActivated.IsChecked.Value;
    this.m_pathPlanningParameters.TimeOutIsActivated = flag;
    this.tbTimeOutValue.IsEnabled = flag;
  }

  private void rbJointWeights_Click(object sender, RoutedEventArgs e)
  {
    bool flag = this.rbJointWeights.IsChecked.Value;
    if (this.m_lastClickType == CUiAPPGeneralSettings.LastClickType.ZONES && flag)
      this.rbZones.IsChecked = new bool?(false);
    this.m_lastClickType = CUiAPPGeneralSettings.LastClickType.JOINT_WEIGHTS;
  }

  private void rbZones_Click(object sender, RoutedEventArgs e)
  {
    bool flag = this.rbZones.IsChecked.Value;
    if (this.m_lastClickType == CUiAPPGeneralSettings.LastClickType.JOINT_WEIGHTS && flag)
      this.rbJointWeights.IsChecked = new bool?(false);
    this.m_lastClickType = CUiAPPGeneralSettings.LastClickType.ZONES;
  }

  private void cbZoneMotionChoice_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    if (!this.m_isParentActivated)
      return;
    this.InitZoneData();
  }

  private void rbSelectMotionTypes_Click(object sender, RoutedEventArgs e)
  {
    if (!this.m_isParentActivated)
      return;
    this.InitZoneData();
  }

  private void ZoneDataDidChanged()
  {
    this.UpdateExistingZones();
    CApAPPRoboticTask selectedItem = this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask;
    this.m_zoneDataByRoboticTask[selectedItem].Sort((IComparer<CApAPPZoneData>) this);
    int selectedIndex1 = this.fgZone.SelectedIndex;
    this.fgZone.ItemsSource = (IEnumerable) null;
    this.fgZone.ItemsSource = (IEnumerable) this.m_zoneDataByRoboticTask[selectedItem];
    if (selectedIndex1 < this.m_zoneDataByRoboticTask[selectedItem].Count)
      this.fgZone.SelectedIndex = selectedIndex1;
    this.fgZone.Refresh();
    this.UpdateZoneButtonVisibility();
    string[][] i_motionZoneNames = new string[this.m_zoneDataByRoboticTask[selectedItem].Count][];
    for (int index1 = 0; index1 < this.m_zoneDataByRoboticTask[selectedItem].Count; ++index1)
    {
      i_motionZoneNames[index1] = new string[this.m_nbZoneColumnVisible];
      for (int index2 = 0; index2 < this.m_nbZoneColumnVisible; ++index2)
        i_motionZoneNames[index1][index2] = this.m_zoneDataByRoboticTask[selectedItem][index1].Names[index2];
    }
    if (this.rbAllMotionTypes.IsChecked.Value)
    {
      selectedItem.SetMotionZoneNames((TxMotionType) 1, i_motionZoneNames);
      selectedItem.SetMotionZoneNames((TxMotionType) 2, i_motionZoneNames);
    }
    else
    {
      int selectedIndex2 = this.cbZoneMotionChoice.SelectedIndex;
      selectedItem.SetMotionZoneNames(selectedIndex2 == 0 ? (TxMotionType) 1 : (TxMotionType) 2, i_motionZoneNames);
    }
  }

  private bool AreMotionZonesValid(List<List<string>> motionZones)
  {
    bool flag = true;
    if (motionZones.Count <= 0)
      flag = false;
    for (int index1 = 0; flag && index1 < motionZones.Count; ++index1)
    {
      List<int> intList = new List<int>();
      for (int index2 = 0; flag && index2 < motionZones[index1].Count; ++index2)
      {
        if (motionZones[index1][index2] != "" && !this.m_zoneNamesByParameter[index2].Contains(motionZones[index1][index2]))
          flag = false;
      }
    }
    return flag;
  }

  private void m_ZoneDataDidChanged(object sender, EventArgs e) => this.ZoneDataDidChanged();

  private void UpdateZoneButtonVisibility()
  {
    if (this.m_zoneNamesByParameter[0].Count == 1 && this.m_zoneNamesByParameter[0][0] == "")
    {
      this.btnRemoveZone.IsEnabled = false;
      this.btnAddZone.IsEnabled = false;
    }
    else
    {
      CApAPPRoboticTask selectedItem = this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask;
      this.btnAddZone.IsEnabled = this.m_zoneDataByRoboticTask[selectedItem].Count != this.m_zoneNamesByParameter[0].Count * this.m_zoneNamesByParameter[1].Count * this.m_zoneNamesByParameter[2].Count;
      this.btnRemoveZone.IsEnabled = this.m_zoneDataByRoboticTask[selectedItem].Count > 0 && this.fgZone.SelectedIndex > 0;
    }
  }

  private void UpdateZoneFlexGrid()
  {
    if (this.m_zoneDataByRoboticTask == null || this.cbRobotTaskNames.SelectedIndex >= this.m_zoneDataByRoboticTask.Count)
      return;
    CApAPPRoboticTask selectedItem = this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask;
    this.fgZone.ItemsSource = (IEnumerable) null;
    this.fgZone.ItemsSource = (IEnumerable) this.m_zoneDataByRoboticTask[selectedItem];
    this.fgZone.Refresh();
    this.m_existingZoneByRoboticTask[selectedItem].Clear();
    for (int index = 0; index < this.m_zoneDataByRoboticTask[selectedItem].Count; ++index)
      this.m_existingZoneByRoboticTask[selectedItem].Add(this.m_zoneDataByRoboticTask[selectedItem][index].UniqueName);
  }

  public List<string> GenerateAvailableZoneNames(int parameterIndex)
  {
    return this.GenerateAvailableZoneNames(this.m_zoneDataByRoboticTask[this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask][this.fgZone.SelectedIndex], parameterIndex);
  }

  public List<string> GenerateAvailableZoneNames(CApAPPZoneData currentZoneData, int parameterIndex)
  {
    List<string> availableZoneNames = new List<string>();
    CApAPPRoboticTask selectedItem = this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask;
    string uniqueName1 = currentZoneData.UniqueName;
    int num1 = 0;
    int num2 = this.m_zoneNamesByParameter[0].Count;
    if (parameterIndex > 0)
    {
      num1 = this.m_zoneNamesByParameter[0].IndexOf(currentZoneData.Name1);
      num2 = num1 + 1;
    }
    int num3 = 0;
    int num4 = this.m_zoneNamesByParameter[1].Count;
    if (parameterIndex > 1)
    {
      num3 = this.m_zoneNamesByParameter[1].IndexOf(currentZoneData.Name2);
      num4 = num3 + 1;
    }
    int num5 = 0;
    int count = this.m_zoneNamesByParameter[2].Count;
    for (int index1 = num1; index1 < num2; ++index1)
    {
      bool flag1 = false;
      for (int index2 = num3; !flag1 && index2 < num4; ++index2)
      {
        bool flag2 = false;
        for (int index3 = num5; !flag2 && index3 < count; ++index3)
        {
          string uniqueName2 = CApAPPZoneData.GenerateUniqueName(this.m_zoneNamesByParameter[0][index1], this.m_zoneNamesByParameter[1][index2], this.m_zoneNamesByParameter[2][index3]);
          if (!this.m_existingZoneByRoboticTask[selectedItem].Contains(uniqueName2) || uniqueName1 == uniqueName2)
          {
            string str = "";
            switch (parameterIndex)
            {
              case 0:
                str = this.m_zoneNamesByParameter[0][index1];
                break;
              case 1:
                str = this.m_zoneNamesByParameter[1][index2];
                break;
              case 2:
                str = this.m_zoneNamesByParameter[2][index3];
                break;
            }
            if (availableZoneNames.IndexOf(str) == -1)
            {
              availableZoneNames.Add(str);
              if (parameterIndex < 2)
                flag2 = true;
              if (parameterIndex < 1)
                flag1 = true;
            }
          }
        }
      }
    }
    return availableZoneNames;
  }

  public List<string> ComputeNextZoneNames(CApAPPZoneData i_dataList)
  {
    List<string> nextZoneNames = new List<string>();
    CApAPPRoboticTask selectedItem = this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask;
    if (i_dataList != null)
    {
      int index1 = this.m_zoneNamesByParameter[0].IndexOf(i_dataList.Name1);
      int index2 = this.m_zoneNamesByParameter[1].IndexOf(i_dataList.Name2);
      int index3 = this.m_zoneNamesByParameter[2].IndexOf(i_dataList.Name3);
      for (bool flag = false; !flag && index1 < this.m_zoneNamesByParameter[0].Count; ++index1)
      {
        while (!flag && index2 < this.m_zoneNamesByParameter[1].Count)
        {
          while (!flag && index3 < this.m_zoneNamesByParameter[2].Count)
          {
            string uniqueName = CApAPPZoneData.GenerateUniqueName(this.m_zoneNamesByParameter[0][index1], this.m_zoneNamesByParameter[1][index2], this.m_zoneNamesByParameter[2][index3]);
            if (this.m_existingZoneByRoboticTask[selectedItem].Contains(uniqueName))
            {
              ++index3;
            }
            else
            {
              nextZoneNames.Add(this.m_zoneNamesByParameter[0][index1]);
              nextZoneNames.Add(this.m_zoneNamesByParameter[1][index2]);
              nextZoneNames.Add(this.m_zoneNamesByParameter[2][index3]);
              flag = true;
            }
          }
          ++index2;
          index3 = 0;
        }
        index2 = 0;
      }
    }
    else
    {
      nextZoneNames.Add(this.m_zoneNamesByParameter[0][0]);
      nextZoneNames.Add(this.m_zoneNamesByParameter[1][0]);
      nextZoneNames.Add(this.m_zoneNamesByParameter[2][0]);
    }
    return nextZoneNames;
  }

  public static int ComputeZoneDataRank(int zone0Index, int zone1Index, int zone2Index)
  {
    int zoneDataRank = 0;
    if (zone0Index >= 0 && zone0Index < 1000)
      zoneDataRank += zone0Index * 1000000;
    if (zone1Index >= 0 && zone1Index < 1000)
      zoneDataRank += zone1Index * 1000;
    if (zone2Index >= 0 && zone2Index < 1000)
      zoneDataRank += zone2Index;
    return zoneDataRank;
  }

  private void UpdateExistingZones()
  {
    CApAPPRoboticTask selectedItem = this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask;
    if (this.m_existingZoneByRoboticTask[selectedItem].Contains(this.m_previousZoneName))
      this.m_existingZoneByRoboticTask[selectedItem].Remove(this.m_previousZoneName);
    this.m_existingZoneByRoboticTask[selectedItem].Clear();
    foreach (CApAPPZoneData capAppZoneData in this.m_zoneDataByRoboticTask[selectedItem])
      this.m_existingZoneByRoboticTask[selectedItem].Add(capAppZoneData.UniqueName);
  }

  private void fgZone_CellEditEnded(object sender, DataGridCellEditEndingEventArgs e)
  {
    if (!this.fgZone.Columns.Contains(e.Column))
      return;
    this.UpdateExistingZones();
  }

  private void fgZone_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    this.UpdateZoneButtonVisibility();
  }

  private void fgZone_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
  {
    int num = this.fgZone.Columns.IndexOf(e.Column);
    if (num < 0 || num >= 3)
      return;
    string info = "";
    switch (num)
    {
      case 0:
        info = "AvailableZoneNames1";
        break;
      case 1:
        info = "AvailableZoneNames2";
        break;
      case 2:
        info = "AvailableZoneNames3";
        break;
    }
    if (info != "")
      this.NotifyPropertyChanged(info);
    this.m_previousZoneName = this.m_zoneDataByRoboticTask[this.cbRobotTaskNames.SelectedItem as CApAPPRoboticTask][this.fgZone.SelectedIndex].UniqueName;
  }

  public int Compare(CApAPPZoneData x, CApAPPZoneData y) => x.UniqueRank.CompareTo(y.UniqueRank);

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/automaticpathplanner/cuiappgeneralsettings.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.spGeneralSettings = (StackPanel) target;
        break;
      case 2:
        this.generalSettingsGrid = (Grid) target;
        break;
      case 3:
        this.cbBackupOperation = (CheckBox) target;
        this.cbBackupOperation.Click += new RoutedEventHandler(this.cbBackupOperation_Click);
        break;
      case 4:
        this.cbTimeOutIsActivated = (CheckBox) target;
        this.cbTimeOutIsActivated.Click += new RoutedEventHandler(this.cbTimeOutIsActivated_Click);
        break;
      case 5:
        this.tbTimeOutValue = (TextBox) target;
        this.tbTimeOutValue.TextChanged += new TextChangedEventHandler(this.tbTimeOutValue_TextChanged);
        this.tbTimeOutValue.PreviewTextInput += new TextCompositionEventHandler(this.tbTimeOutValue_PreviewTextInput);
        this.tbTimeOutValue.IsKeyboardFocusedChanged += new DependencyPropertyChangedEventHandler(this.tbTimeOutValue_FocusChanged);
        break;
      case 6:
        this.tbTimeOutValueUnit = (TextBlock) target;
        break;
      case 7:
        ((ButtonBase) target).Click += new RoutedEventHandler(this.btnRestoreDefaults_Click);
        break;
      case 8:
        this.tbRobotNames = (TextBlock) target;
        break;
      case 9:
        this.cbRobotTaskNames = (ComboBox) target;
        this.cbRobotTaskNames.DropDownClosed += new EventHandler(this.cbRobotTaskNames_DropDownClosed);
        break;
      case 10:
        this.gRobot = (Grid) target;
        break;
      case 11:
        this.rbJointWeights = (CheckBox) target;
        this.rbJointWeights.Click += new RoutedEventHandler(this.rbJointWeights_Click);
        break;
      case 12:
        this.rbZones = (CheckBox) target;
        this.rbZones.Click += new RoutedEventHandler(this.rbZones_Click);
        break;
      case 13:
        this.fgJointsWeight_DataGrid = (DataGrid) target;
        break;
      case 14:
        this.tbGunWeight = (TextBlock) target;
        break;
      case 15:
        this.fgGunJointsWeight_DataGrid = (DataGrid) target;
        break;
      case 16 /*0x10*/:
        this.rbAllMotionTypes = (RadioButton) target;
        this.rbAllMotionTypes.Click += new RoutedEventHandler(this.rbSelectMotionTypes_Click);
        break;
      case 17:
        this.rbSelectMotionTypes = (RadioButton) target;
        this.rbSelectMotionTypes.Click += new RoutedEventHandler(this.rbSelectMotionTypes_Click);
        break;
      case 18:
        this.cbZoneMotionChoice = (ComboBox) target;
        this.cbZoneMotionChoice.SelectionChanged += new SelectionChangedEventHandler(this.cbZoneMotionChoice_SelectionChanged);
        break;
      case 19:
        this.fgZone = (DataGrid) target;
        this.fgZone.BeginningEdit += new EventHandler<DataGridBeginningEditEventArgs>(this.fgZone_BeginningEdit);
        this.fgZone.CellEditEnding += new EventHandler<DataGridCellEditEndingEventArgs>(this.fgZone_CellEditEnded);
        this.fgZone.SelectionChanged += new SelectionChangedEventHandler(this.fgZone_SelectionChanged);
        break;
      case 23:
        this.btnRemoveZone = (Button) target;
        this.btnRemoveZone.Click += new RoutedEventHandler(this.btnRemoveZone_Click);
        break;
      case 24:
        this.btnAddZone = (Button) target;
        this.btnAddZone.Click += new RoutedEventHandler(this.btnAddZone_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IStyleConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 20:
        ((ComboBox) target).DropDownOpened += new EventHandler(this.cbZoneFlexGrid_DropDown);
        ((ComboBox) target).DropDownClosed += new EventHandler(this.cbZoneFlexGrid_DropdownClosed);
        break;
      case 21:
        ((ComboBox) target).DropDownOpened += new EventHandler(this.cbZoneFlexGrid_DropDown);
        ((ComboBox) target).DropDownClosed += new EventHandler(this.cbZoneFlexGrid_DropdownClosed);
        break;
      case 22:
        ((ComboBox) target).DropDownOpened += new EventHandler(this.cbZoneFlexGrid_DropDown);
        ((ComboBox) target).DropDownClosed += new EventHandler(this.cbZoneFlexGrid_DropdownClosed);
        break;
    }
  }

  private enum LastClickType
  {
    NONE,
    JOINT_WEIGHTS,
    ZONES,
  }
}
